<?php
/**
 * Anything added to this helper will be available to all templates in the Pages controller.
 */
 /**
  * create an editable region
  * @param int $page_id
  * @param string $section name of section in table
  * @param boolean $admin_edit tells function which style of
  * div to draw, if in admin section, it puts a blue border around section  
  */

function create_region($page_id,$section,$admin_edit=false,$default_content='')
{
      // see if region exists
      $page_content =& new Page_Content();
      $id = $page_content->find(array(
                               'search'=>'first',
                               'conditions'=>array('page_id=? AND section=?',$page_id,$section),
                               'fields'=>'id'
                                      )
                        );
      if(!empty($id)){
          $page_content->load($id['id']);
      } else {
          // create section and then create it              
          $fields = array(
                           'page_id'=>$page_id,
                           'section'=>$section                              
                          );
          $page_content->set('page_id',$page_id);
          $page_content->set('section',$section);
          $page_content->save();
      }
      if($admin_edit){
          $content = $page_content->get('content'); 
          if ( empty($content) )
          {
            $content = empty($default_content) ? '<i>you can add content here</i>' : $default_content;
          }           
          echo "
              <div style='border: 1px solid #00CCFF;background:#ccFFFF;_height:1%'>   
              <form action='".url_for('edit_content_area')."' method='get' style='display:inline;'>
              <input type='hidden' name='page_id' value='$page_id'>
              <input type='hidden' name='section' value='$section'>
              <input type='hidden' name='action' value='edit_region'>
              <input type='submit' value='EDIT' style='background:green;border:1px solid #fff;color:#fff;font-weight:bold;font-size:11px;padding:2px;cursor:hand;width:55px'>
              </form>$content</div>
              ";
      } else {
          echo $page_content->get('content');
      }
 }
 
 function get_my_child_pages($id)
 {
    $pages = new Pages();
    return $pages->find(array(
                               'conditions'=>array('parent_id=?',$id),
                               'fields'=>array('title','index_name')
                                     )
                              );
 }
 function get_my_parent_page($id)
 {
    $pages = new Pages();
    $parent = $pages->find(array(
                               'search'=>'first',
                               'conditions'=>array('id=?',$id),
                               'fields'=>array('parent_id')
                                     )
                              );
     return $pages->find(array(
                               'conditions'=>array('id=?',$parent['parent_id']),
                               'fields'=>array('title','index_name')
                                     )
                              );                           
 }
 
 function pages_to_ul($parent_id=0)
 {
     $pages = new Pages();
     return $pages->pages_to_ul($parent_id);
 }
 
 function pages_to_list_options($parent_id=0,$level=0)
 {
     $pages = new Pages();
     return $pages->pages_to_list_options($parent_id,$level);
 }
 
 function load_page_template($file)
 {
     if ( !empty($file) && !is_null($file) )
     {
         return VIEWS.'/admin/pages/templates/'.basename($file);
     }
 }
?>